<script setup lang="ts">
defineOptions({
  name: 'Category',
})
// 引入接口方法
import { onMounted } from 'vue'
import useCategoryStore from '@/store/modules/category.ts'

let categoryStore = useCategoryStore()
// 存储一级分类的数据
onMounted(() => {
  getC1()
})
const getC1 = () => {
  // 分类全局组件挂载完毕，通知仓库发请求获取一级分类
  categoryStore.getC1()
}
// 通知仓库发请求获取二级分类
const handler = () => {
  // 清空二级和三级分类
  categoryStore.c2Id = ''
  // categoryStore.c2Arr = [] 这个没必要清空，之后会重新请求被覆盖
  categoryStore.c3Id = ''
  categoryStore.c3Arr = []
  categoryStore.getC2()
}
// 通知仓库发请求获取三级分类
const handler1 = () => {
  categoryStore.c3Id = ''
  // categoryStore.c3Arr = [] 这个没必要清空，之后会重新请求被覆盖
  categoryStore.getC3()
}
// 接收父组件传递过来的scene
defineProps(['scene'])
</script>

<template>
  <el-card>
    <el-form inline>
      <el-form-item label="一级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          style="width: 240px"
          v-model="categoryStore.c1Id"
          @change="handler"
        >
          <!--          el-option  label为显示文字，value为select下拉菜单收集的数据 -->
          <!--            v-for="(c1, index) in categoryStore.c1Arr"-->
          <el-option
            v-for="c1 in categoryStore.c1Arr"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          style="width: 240px"
          v-model="categoryStore.c2Id"
          @change="handler1"
        >
          <el-option
            v-for="c2 in categoryStore.c2Arr"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          style="width: 240px"
          v-model="categoryStore.c3Id"
        >
          <el-option
            v-for="c3 in categoryStore.c3Arr"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<style scoped lang="scss"></style>
